package pers.corvey.studyjava.aspect;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;

import pers.corvey.studyjava.exception.BadRequestException;
import pers.corvey.studyjava.exception.DAOException;
import pers.corvey.studyjava.exception.NotFoundException;

@Aspect
@Component
public class DAOAspect {

    @Pointcut("execution(public * pers.corvey.studyjava.dao.*.* (..) )")
    private void daoPointCut() {}
    
    @AfterThrowing(throwing="e", pointcut="daoPointCut()")
    public void throwDAOException(Throwable e) {
        System.out.println("捕捉到了DAO的异常");
        if (e instanceof EmptyResultDataAccessException) {
            throw new BadRequestException("操作失败，不存在相应的记录！", e);
        }
        throw new DAOException("操作失败，服务器发生错误！", e);
    }
    
}
